﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LinqToExcel;
using System.Runtime.Serialization;
using MensUnoShop.Models;

namespace MensUnoShop.Areas.Admin.Models
{
    public class PictureImport : MensUnoImport, IImport
    {
        public PictureImport(HttpPostedFileBase file, ImportType it) : base(file, it) { }
        public bool IMPORT(ref string result)
        {
            bool isok = false;
            try
            {
                var execlFile = new ExcelQueryFactory(_filepath);
                //新增圖說
                execlFile.AddMapping<PictureData>(d => d.MarketSN, "貨號");
                execlFile.AddMapping<PictureData>(d => d.PicPath, "圖片路徑");
                execlFile.AddMapping<PictureData>(d => d.PicNote, "圖說");
                var excel = execlFile.Worksheet<PictureData>("Picture");
                foreach (var pic in excel)
                {
                    MarketFactory.ImportMarketPic(pic.MarketSN, pic.PicPath, pic.PicNote);
                }


                result = "Success Update!!!";
                isok = true;


            }
            catch (Exception ex)
            {
                result = ex.ToString();
                isok = false;
            }
            return isok;
        }
    }
    [DataContract]
    public class PictureData
    {
        [DataMember(Order = 1)]
        public string MarketSN { set; get; }
        [DataMember(Order = 2)]
        public string PicPath { set; get; }
        [DataMember(Order = 3)]
        public string PicNote { set; get; }
    }
}